Overview
TheCoCaLoss class extends ClipLoss to support training CoCa models, which combine contrastive learning with autoregressive caption generation. It computes a weighted combination of:
- Contrastive loss - CLIP-style image-text matching (inherited from ClipLoss)
- Caption loss - Cross-entropy loss for next-token prediction in caption generation
Class Definition
Initialization Parameters
float
required
Weight applied to the caption generation loss. Controls the relative importance of caption quality vs. contrastive matching.
float
required
Weight applied to the contrastive loss. Set to 0 to train only the caption decoder.
int
default:"0"
Padding token ID. Positions with this token are ignored when computing caption loss.
bool
default:"False"
If True, computes contrastive loss only between local and gathered features. See ClipLoss documentation for details.
bool
default:"False"
If True, gathers features with gradient flow enabled for contrastive loss.
bool
default:"False"
If True, caches ground truth labels for contrastive loss.
int
default:"0"
Current process rank in distributed training.
int
default:"1"
Total number of processes in distributed training.
bool
default:"False"
If True, uses Horovod for distributed operations instead of torch.distributed.
Attributes
Inherits all attributes fromClipLoss, plus:
- clip_loss_weight: Weight for contrastive loss component
- caption_loss_weight: Weight for caption generation loss component
- caption_loss: CrossEntropyLoss module with
ignore_index=pad_id
Key Methods
forward
image_features: Normalized contrastive image features of shape(batch_size, embed_dim)text_features: Normalized contrastive text features of shape(batch_size, embed_dim)logits: Caption generation logits of shape(batch_size, seq_len, vocab_size)labels: Target token IDs for caption generation of shape(batch_size, seq_len)logit_scale: Temperature parameter for contrastive loss (typicallymodel.logit_scale.exp())output_dict: If True, returns dict with named losses, else returns tuple
- If
output_dict=False: Tuple of(clip_loss, caption_loss)- both weighted by their respective coefficients - If
output_dict=True: Dictionary with keys"contrastive_loss"and"caption_loss"
Usage Example
Distributed Training Example
Dictionary Output for Logging
Caption-Only Training
Custom Loss Weighting Strategy
Monitoring Loss Components
Mathematical Formulation
The total CoCa loss is: Where:- Contrastive Loss (inherited from ClipLoss):
-
Caption Loss (cross-entropy with teacher forcing):
Where:
- = batch size
- = sequence length
- = target token at position
- = image features
- Padding tokens are ignored via
ignore_index
Hyperparameter Tuning
Recommended loss weight ratios:
Guidelines:
- Start with
caption_loss_weight=2.0,clip_loss_weight=1.0 - If captions are low quality, increase
caption_loss_weight - If retrieval performance is poor, increase
clip_loss_weight - Monitor both loss components separately
Related
- CoCa - Model that uses this loss
- ClipLoss - Parent class for contrastive loss
- Training Guide - Full training examples
